﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MVVM.Demo.StockProvider;
using System.Collections.ObjectModel;

namespace MVVM.Demo.Client
{
    /// <summary>
    /// Interaction logic for WatchListConventional.xaml
    /// </summary>
    public partial class WatchListConventional : Window
    {
        private readonly RandomQuoteSource _quoteSource = new RandomQuoteSource();
        private ObservableCollection<Quote> _quotes = new ObservableCollection<Quote>();
        
        public WatchListConventional()
        {
            InitializeComponent();

            _quoteSource.QuoteArrived += new Action<Quote>(_quoteSource_QuoteArrived);
            this.DataContext = _quotes;
        }

        void _quoteSource_QuoteArrived(Quote quote)
        {
            Action dispatchAction = () => _quotes.Add(quote);
            this.Dispatcher.BeginInvoke(dispatchAction);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string symbol = _symbolText.Text;
            _quoteSource.Subscribe(symbol);
            _lastsymbolText.Text = symbol;
        }
    }
}
